'''
车辆管理相关页面测
'''

from Lib.base.page_object.enterprise.enterprise_manage import EnterpriseManager
from Lib.base.page_object.safety_production.task import Task
from Lib.base.page_object.safety_production.event import Event
from Lib.base.page_object.safety_production.home import Home
from selenium.webdriver.common.by import By
import allure
import os
from Lib.common.assert_class import AssertClass
import pytest
from Lib.common.get_data_by_mysql import MysqlAssert


@pytest.fixture(scope='class', autouse=True)
@pytest.mark.usefixtures('login')
def set_cls(login):
    TestSafetyProduction.enterprise = EnterpriseManager(login)
    TestSafetyProduction.assert_class = AssertClass(login)
    TestSafetyProduction.task = Task(login)
    TestSafetyProduction.event = Event(login)
    TestSafetyProduction.home = Home(login)


@allure.feature('安全生产企业相关页面测试')
class TestSafetyProduction:

    @allure.story("执法任务")
    @allure.title("新建执法任务")
    def test_01_create_task(self, login):
        with allure.step('为测试部门进行排序'):
            self.enterprise.department_sort()
            # 设置断言
            assert_arg = (By.XPATH, '//p[text()="排序成功"]')
            self.assert_class.make_assert_by_text(args=assert_arg, assert_data='排序成功', context='编辑部门')
        with allure.step('新建企业'):
            self.enterprise.create_enterprise()
            # 设置断言
            assert_args = (By.XPATH, '//p[contains(text(), "新建成功")]')
            self.assert_class.make_assert_by_text(args=assert_args, assert_data='新建成功')
        with allure.step('新建任务'):
            self.task.create_task()
            # 设置断言
            assert_args = (By.XPATH, '//div[@class="global-message-card"]//div[text()="已下达"]')
            self.assert_class.make_assert_by_text(args=assert_args, assert_data='已下达')

    @allure.story("执法任务")
    @allure.title("执法任务筛选列表")
    def test_02_task_list(self, login):
        with allure.step('根据任务来源进行筛选'):
            self.task.task_list_by_source()
            # 设置断言
            assert_args = (By.XPATH, '//tbody//tr/td[8]//span')
            self.assert_class.make_assert_by_elements_text(args=assert_args, assert_data='系统新建')
        with allure.step('根据任务阶段进行筛选'):
            self.task.task_list_by_stage()
            # 设置断言
            assert_args = (By.XPATH, '//tbody//tr/td[6]//span')
            self.assert_class.make_assert_by_elements_text(args=assert_args, assert_data='受理')
        with allure.step('根据任务状态进行筛选'):
            self.task.task_list_by_status()
            # 设置断言
            assert_args = (By.XPATH, '//tbody//tr/td[7]//span')
            self.assert_class.make_assert_by_elements_text(args=assert_args, assert_data='已下达')

    @allure.story("执法任务")
    @allure.title("执法任务列表排序")
    def test_03_task_sort(self, login):
        self.task.task_list_sort()
        # 设置断言
        assert_args1 = (By.XPATH, '//tbody//tr[1]/td[3]//span')
        assert_args2 = (By.XPATH, '//tbody//tr[2]/td[3]//span')
        self.assert_class.make_assert_by_compare_time(args1=assert_args1, args2=assert_args2)

    @allure.story("执法任务")
    @allure.title("执法任务搜索")
    def test_04_task_search(self, login):
        with allure.step('错误搜索'):
            self.task.task_search_error()
            # 设置断言
            assert_args = (By.XPATH, '//p[contains(text(), "暂无数据")]')
            self.assert_class.make_assert_by_text(args=assert_args, assert_data='暂无数据')
        with allure.step('按任务编号和执法对象进行搜索'):
            self.task.task_search()
            # 设置断言
            assert_args = (By.XPATH, '//tbody//tr[1]/td[4]//span')
            self.assert_class.make_assert_by_text(args=assert_args, assert_data='UI新建')

    @allure.story("执法任务")
    @allure.title("执法任务受理")
    def test_task_accept(self, login):
        with allure.step('执法任务受理'):
            self.task.task_detail_accept()
            assert_args = (By.XPATH, '//div[@title="已受理"]//div[text()="已受理"]')
            self.assert_class.make_assert_by_text(args=assert_args, assert_data='已受理')

    @allure.story("执法任务")
    @allure.title("执法任务检查")
    def test_task_inspect(self, login):
        with allure.step('执法任务检查'):
            self.task.task_detail_inspect()
            assert_args = (By.XPATH, '//div[@title="已检查"]//div[text()="已检查"]')
            self.assert_class.make_assert_by_text(args=assert_args, assert_data='已检查')

    @allure.story("执法任务")
    @allure.title("执法任务复查")
    def test_task_review(self, login):
        with allure.step('执法任务复查'):
            self.task.task_detail_review()
            assert_args = (By.XPATH, '//div[@title="已复查"]//div[text()="已复查"]')
            self.assert_class.make_assert_by_text(args=assert_args, assert_data='已复查')

    @allure.story("执法任务")
    @allure.title("执法任务归档")
    def test_task_pigeonhole(self, login):
        with allure.step('执法任务归档'):
            self.task.task_detail_pigeonhole()
            assert_args = (By.XPATH, '//div[@title="已归档"]//div[text()="已归档"]')
            self.assert_class.make_assert_by_text(args=assert_args, assert_data='已归档')

    @allure.story("执法任务")
    @allure.title("执法任务退单")
    def test_06_task_chargeback(self, login):
        with allure.step('新建执法任务'):
            self.task.create_task()
            # 设置断言
            assert_args = (By.XPATH, '//div[@class="global-message-card"]//div[text()="已下达"]')
            self.assert_class.make_assert_by_text(args=assert_args, assert_data='已下达')
        with allure.step('执法任务检查'):
            self.task.task_chargeback()
            assert_args = (By.XPATH, '//div[@title="已退单"]//div[text()="已退单"]')
            self.assert_class.make_assert_by_text(args=assert_args, assert_data='已退单')

    @allure.story("事件列表")
    @allure.title("新建安全相关的事件")
    # @pytest.mark.skip
    def test_07_event_create(self, login):
        self.event.create_event()
        # 设置断言
        assert_args = (By.XPATH, '//p[contains(text(), "事件上报成功")]')
        self.assert_class.make_assert_by_text(args=assert_args, assert_data='事件上报成功')

    @allure.story("事件列表")
    @allure.title("安全相关的事件列表")
    # @pytest.mark.skip
    def test_08_event_list(self, login):
        with allure.step('查看页面显示事件类型'):
            self.event.event_list()
            # 设置断言
            assert_args = (By.XPATH, '//tbody/tr//span[text()="安全生产"]')
            self.assert_class.make_assert_by_elements_text(args=assert_args, assert_data='安全生产')
        with allure.step('搜索事件并查看事件详情'):
            self.event.event_search_and_detail()
            # 设置断言
            assert_args = (By.XPATH, '//div[text()="UI新建单位"]')
            self.assert_class.make_assert_by_elements_text(args=assert_args, assert_data='UI新建单位')

    @allure.story("首页")
    @allure.title("搜企业")
    # @pytest.mark.skip
    def test_09_home_search_enterprise(self, login):
        with allure.step('搜企业后查看企业详情'):
            self.home.search_enterprise_detail()
            # 设置断言
            assert_args = (By.XPATH, '//div[@class="xgrid__block"]//span[text()="UI新建单位"]')
            self.assert_class.make_assert_by_text(args=assert_args, assert_data='UI新建单位')
        with allure.step('搜企业后查看事件列表'):
            self.home.search_enterprise_and_event()
            # 设置断言
            assert_args = (By.XPATH, '//div[@class="case-home"]//tbody//span[text()="UI新建的安全生产事件"]')
            self.assert_class.make_assert_by_elements_text(args=assert_args, assert_data='UI新建的安全生产事件')
        with allure.step('搜企业后查看执法任务列表'):
            self.home.search_enterprise_and_task()
            # 设置断言
            assert_args = (By.XPATH, '//span[text()="UI新建企业任务名称"]')
            self.assert_class.make_assert_by_elements_text(args=assert_args, assert_data='UI新建企业任务名称')

    @allure.story("首页")
    @allure.title("搜事件")
    # @pytest.mark.skip
    def test_10_home_search_event(self, login):
        with allure.step('进入搜事件页面，点击最新违法事件撒点查看详情'):
            self.home.search_event_detail()
            # 设置断言
            assert_args = (By.XPATH, '//div[text()="UI新建单位"]')
            self.assert_class.make_assert_by_text(args=assert_args, assert_data='UI新建单位')
        with allure.step('点击更多进入违法事件列表'):
            self.home.search_event_list()
            # 设置断言
            assert_args = (By.XPATH, '//tbody/tr//span[text()="安全生产"]')
            self.assert_class.make_assert_by_text(args=assert_args, assert_data='安全生产')


if __name__ == '__main__':
    pytest.main(
        ['-v', '-s', '--reruns=3', '--reruns-delay=1',
         'safety_production_test.py::TestSafetyProduction'])
    # pytest.main(['-v', './car_test.py', '--alluredir', '../result', '--clean-alluredir'])
    # os.system(r'copy ..\config\environment.properties ..\result\environment.properties')
    # os.system('allure generate  ../result -o ../report/ --clean')
